package code;
import java.io.IOException;

import global.EID;
import global.PID;
import global.RID;
import global.SystemDefs;
import global.TID;
import heap.FieldNumberOutOfBoundException;
import heap.HFBufMgrException;
import heap.HFDiskMgrException;
import heap.HFException;
import heap.InvalidSlotNumberException;
import heap.InvalidTupleSizeException;
import heap.InvalidTypeException;
import heap.LabelHeapfile;
import heap.SpaceNotAvailableException;
import heap.StringLabel;
import heap.Triple;
import heap.TripleHeapfile;
import heap.Tuple;



public class Interface2 {
	public static LabelHeapfile entityHeap;
	public static LabelHeapfile predicateHeap;
	public static TripleHeapfile tripleHeap;
	public static SystemDefs sysdef;
	static {
			try {
				int NUMBUF = 100; 
				int NUMPAGES = 1000;
				String dbpath = "/tmp/dbtest";
				sysdef = new SystemDefs( dbpath, NUMPAGES, NUMBUF, "Clock" );
				System.out.println("Creating heap files");
				entityHeap = new LabelHeapfile("entities");
				predicateHeap = new LabelHeapfile("predicates");
				tripleHeap = new TripleHeapfile("triples");				
			} catch (HFException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (HFBufMgrException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (HFDiskMgrException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
	}
	
	public static void init () {
		
	}
	
	
	public static void insertTriple (String subject, String predicate, String object) throws Exception {
		/*
		 * 1. Insert subject, predicate and object and get the rid's
		 * 2. Insert them into tripleHeapFile
		 */
		
		// Search through the entity heap file and get the eid.
		// If no eid, insert and get an eid. SET the flag INSERT true
		// Search through the predicate heap file and get the pid
		// If no pid, insert and get an pid. set the flag INSERT true
		// Use SPO index and check if the combination exist
		// If the combination exists. get the old confidence
		// if no combination exist set the flag INSERT true
		// If new confidence is lesser than old confidence, update tid
		// If INSERT: insert triple.
		
		
		System.out.println("Creating String labels");
		StringLabel subject_label = new StringLabel ();
		StringLabel predicate_label = new StringLabel ();
		StringLabel object_label = new StringLabel ();
		System.out.println("Adding string to labels");
		subject_label.addLabel(subject);
		predicate_label.addLabel(predicate);
		object_label.addLabel(object);
		System.out.println("Inserting records");
		RID seid = entityHeap.insertRecord(subject_label.getTupleByteArray());
		System.out.println("ID is:" + seid);
		RID pid = predicateHeap.insertRecord(predicate_label.getTupleByteArray());
		System.out.println("ID is:" + pid);
		RID oeid = entityHeap.insertRecord(object_label.getTupleByteArray());
		System.out.println("ID is:" + oeid);
		Triple triple = new Triple();
		triple.addTriple(seid, pid, oeid, (float).9);
		RID rid = tripleHeap.insertRecord(triple.getTupleByteArray());
		System.out.println("ID is:" + rid);
		
		System.out.println("Reading the inserted data.....");
		Tuple t = tripleHeap.getRecord(rid);
		triple.tupleCopy(t);
		EID _seid = triple.getSubjectID();
		PID _pid = triple.getPredicateID();
		EID _oeid = triple.getObjectID();
		subject_label = entityHeap.getRecord(_seid);
		predicate_label = predicateHeap.getRecord(_pid);
		object_label = entityHeap.getRecord(_oeid);
		System.out.println(subject_label +"," + predicate_label + "," + object_label);	
		PCounter.getStatistics();
	}
	
	
	public static void main (String args []) throws Exception {
		insertTriple("Archana",	"eats", "apple");
		/*
		LabelHeapfile lfile = new LabelHeapfile ("file2");
		StringLabel label = new StringLabel();
		label.addLabel("Archana");
		RID rid = lfile.insertRecord(label.returnTupleByteArray());
		System.out.println("RID:" + rid);
		System.out.println("Record Count:" + lfile.getRecCnt());
		Tuple label2 = lfile.getRecord(rid);
		StringLabel value = new StringLabel ();
		value.tupleCopy(label2);
		System.out.println("Inserted data is:" + value.getStrFld(1));
		*/
	}
	
}
